package client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class BtClient extends Form implements Runnable{

	LocalDevice mydev;
	DiscoveryAgent myAgent;
	
	DataInputStream in;
	DataOutputStream out;


	public BtClient( String title ) {
		super( title );
		
	}
	public void start()
	{
		Thread thr = new Thread(this);
		thr.start();
	}
	public void run()
	{
		try
		{
			System.out.println("start BT client..");

			mydev= LocalDevice.getLocalDevice();
			myAgent= mydev.getDiscoveryAgent();
			
			String conStrClient;
			StreamConnection conn;
			
			conStrClient= myAgent.selectService(new UUID("dcb91cb0638911dbbd130800200c9a66",false),ServiceRecord.AUTHENTICATE_ENCRYPT,false);
					
				
			conn = (StreamConnection)Connector.open(conStrClient);
			
			if (conn!=null)
			{
				//in/out streams
				in= conn.openDataInputStream();
				out= conn.openDataOutputStream();
				
				// endlosschleife
				while (true)
				{
					//read msg
					String inMsg= in.readUTF();
					System.out.println(inMsg);
					this.append(inMsg);
				
					// send answermsg
					out.writeUTF( ". msg form client" );
				}				
			}
		}
		catch (Exception e)
		{ 
			System.out.println(e.getMessage());
		}
	}
}